package xiaohao.order.manager.analyze.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.google.common.collect.Maps;
import com.taobao.api.domain.PromotionDetail;
import com.taobao.api.domain.Trade;

import xiaohao.order.domain.analyze.SpendingDO;
import xiaohao.order.manager.analyze.PromotionAnalyzeManager;

public class DefaultPromotionAnalyzeManager implements PromotionAnalyzeManager {

	private final Logger logger = Logger.getLogger(getClass());

	@Override
	public Map<String, SpendingDO> analyzePromotionByName(List<Trade> trades) {
		Map<String, SpendingDO> map = Maps.newHashMap();
		for (Trade trade : trades) {
			List<PromotionDetail> promotionDetails = trade.getPromotionDetails();
			if (promotionDetails != null && !promotionDetails.isEmpty()) {
				for (PromotionDetail promotionDetail : promotionDetails) {
					String name = promotionDetail.getPromotionName();
					if (StringUtils.isNotEmpty(name)) {
						SpendingDO spendingDO = null;
						
						if (map.get(name) != null) {
							spendingDO = map.get(name);
						} else {
							spendingDO = new SpendingDO();
						}
						
						spendingDO.addOrderAmount();
						map.put(name, spendingDO);
					}
				}
			}
		}
		return map;
	}
	
}
